<?php
/**
 * Created by PhpStorm.
 * User: jifee8505
 * Date: 2018/3/21
 * Time: 下午12:57
 */

namespace common\models\messages;

use Yii;

/**
 * This is the model class for table "{{%user_message}}".
 *
 * @property int $id 自增ID
 * @property int $message_id 消息ID
 * @property string $title 标题
 * @property string $content 内容
 * @property string $user_guid 用户唯一标示ID
 * @property string $details_link 详情链接
 * @property int $status 状态(0未读/1已读/2删除)
 * @property int $created_at 添加时间
 * @property int $updated_at 更新时间
 */
class UserMessage extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%user_message}}';
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', 'ID'),
            'message_id' => Yii::t('app', '消息ID'),
            'title' => Yii::t('app', '标题'),
            'content' => Yii::t('app', '内容'),
            'user_guid' => Yii::t('app', '用户ID'),
            'details_link' => Yii::t('app', '链接'),
            'status' => Yii::t('app', '状态'),
            'created_at' => Yii::t('app', '添加时间'),
            'updated_at' => Yii::t('app', '更新时间'),
        ];
    }

    public static function pushUserMessage($user_guid, $message)
    {
        $model = new UserMessage();

        $model->message_id = $message['id'];
        $model->user_guid = $user_guid;
        $model->title = $message['title'];
        $model->content = $message['content'];
        $model->details_link = $message['details_link'];
        $model->created_at = $message['push_at'];
        $model->updated_at = $message['push_at'];

        if ($model->save()) {
            return true;
        }
        return false;
    }
}